<template>
  <div class="view">
    <!-- 无视频模板 -->
    <div v-if="postData.type ===1" class="mytype1">
      <div class="header">
        <van-icon name="arrow-left" @click="$router.push('/index')" />
        <div
          @click="handleFocus"
          :class="['focus',{myfocus:!postData.has_follow}]"
        >{{postData.has_follow?'已关注':"关注"}}</div>
      </div>
      <div class="content">
        <h3>{{postData.title}}</h3>
        <span>{{postData.user.nickname}}</span>
        <span>{{postData.create_date.split('T')[0]}}</span>
        <p v-html="postData.content"></p>
      </div>
    </div>
    <!-- 有视频的模板 -->
    <div v-if="postData.type ===2" class="mytype2">
      <div class="header">
        <van-icon name="arrow-left" @click="$router.push('/index')" />
      </div>
      <video
        controls
        src="https://video.pearvideo.com/mp4/short/20200629/cont-1672090-15230913-hd.mp4"
        class="video"
      ></video>
      <div class="header">
        <div class="info">
          <img :src="$http.defaults.baseURL+postData.user.head_img" alt />
          {{postData.user.nickname}}
        </div>
        <div
          @click="handleFocus"
          :class="['focus',{myfocus:!postData.has_follow}]"
        >{{postData.has_follow?'已关注':"关注"}}</div>
      </div>
      <div class="content">
        <p>{{postData.title}}</p>
      </div>
    </div>
    <div class="good_share">
      <div @click="handleLike">
        <span :class="['iconfont','icondianzan',{dianzan:postData.has_like}]"></span>
        {{postData.like_length}}
      </div>
      <div>
        <span class="iconfont iconweixin" style="color:#02c802"></span>
        微信
      </div>
    </div>

    <!-- 分隔 -->
    <div class="comment">
      <h3>精彩跟帖</h3>
      <!-- 评论 -->
      <Comment
        @replyComment="replyComment"
        v-if="commentList.length!==0"
        :commentData="item"
        v-for="item in commentList"
        :key="item.id"
      />
    </div>
    <div
      v-if="commentList.length===3"
      class="more"
      @click="$router.push('/moreComment/'+$route.params.id)"
    >
      <p>更多跟帖</p>
    </div>
    <div v-if="commentList.length==0" class="nocomment">
      <p>暂无跟帖，抢占沙发</p>
    </div>
    <!-- 底部 -->
    <MyFooter :commentLength="commentLength" @sendComment="sendComment" ref="myfooterRef" :placeholderText="placeholderText" />
  </div>
</template>
<script>
import Comment from '../components/Comment/Index'
import MyFooter from '../components/Comment/Footer'
export default {
  components: {
    Comment,
    MyFooter
  },
  data() {
    return {
      flag: true,
      postData: {},
      commentList: [],
      CommentId:'',
      placeholderText:'写评论'
    }
  },
  computed: {
    commentLength(){
      return this.commentList.length
    }
  },
  created() {
    this.getpost()
    this.getCommentList()
  },
  methods: {
    async getpost() {
      const { data: res } = await this.$http.get(
        '/post/' + this.$route.params.id
      )
      console.log(res)
      this.postData = res.data
    },
    //取消关注
    handleFocus() {
      if (this.postData.has_follow) {
        //取消关注
        this.$http.get('/user_unfollow/' + this.postData.user.id)
      } else {
        //关注
        this.$http.get('/user_follows/' + this.postData.user.id)
      }
      this.postData.has_follow = !this.postData.has_follow
    },
    //点赞
    async handleLike() {
      const { data: res } = await this.$http.get(
        'post_like/' + this.postData.id
      )
      if (res.message === '点赞成功') {
        this.postData.has_like = true
        this.postData.like_length += 1
      } else {
        this.postData.has_like = false
        this.postData.like_length -= 1
      }
    },
    //获取评论列表
    async getCommentList() {
      const { data: res } = await this.$http.get(
        '/post_comment/' + this.$route.params.id
      )
      if (res.data.length > 3) {
        const arr = res.data
        arr.length = 3
        this.commentList = arr
      } else {
        this.commentList = res.data
      }
      console.log(this.commentList)
    },
    //发布评论
    async sendComment(value){
      let data={
        content:value.trim()
      }
      if(this.CommentId){
        data.parent_id=this.CommentId
      }
      const {data:res}= await this.$http.post('/post_comment/'+this.$route.params.id,data)
      if(res.message === '评论发布成功'){
        this.getCommentList()
        this.CommentId = ''
        this.placeholderText="写评论"
      }
    },
    // 点击回复响应事件
    replyComment(obj){
      // 接收传过来的数据
      this.CommentId = obj.id
      this.placeholderText = obj.nickname
      // 调用子组件的方法，让文本域显示
      this.$refs.myfooterRef.myfocus()
    }
  }
}
</script>
<style lang="less" scoped>
.view{
  padding-bottom:11.667vw;
}
.nocomment {
  height: 22.222vw;
  // background: pink;
  font-size: 3.889vw;
  color: #e4e4e4;
  line-height: 16.667vw;
  text-align: center;
  border-bottom: 0.278vw solid #eee;
  margin-bottom: 4.167vw;
}
.mytype1 {
  .header {
    padding: 0 4.167vw;
    height: 13.889vw;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .van-icon {
      color: blue;
    }
    .focus {
      background: #f2f2f2;
      padding: 1.389vw 2.778vw;
      border-radius: 4.167vw;
      font-size: 3.333vw;
    }
  }
  .content {
    padding: 1.389vw 4.167vw;
    span {
      font-size: 3.889vw;
      color: #999;
      // margin: 0 5.556vw 8.333vw 0;
      margin-right: 5.556vw;
      line-height: 8.333vw;
    }
    p {
      margin-top: 2.778vw;
      font-size: 3.889vw;
      line-height: 6.944vw;
    }
  }
}
.good_share {
  height: 19.444vw;

  display: flex;
  justify-content: center;
  align-items: center;
  border-bottom: 1.389vw solid #e4e4e4;
  div {
    height: 6.944vw;
    line-height: 6.944vw;
    // background-color: orange;
    padding: 0vw 2.778vw;
    border: 0.278vw solid #ccc;
    border-radius: 4.167vw;
    font-size: 3.889vw;
    margin: 0 auto;
    span {
      margin: 0 0.833vw;
    }
  }
}
.comment {
  h3 {
    height: 30px;
    text-align: center;
    padding-top: 20px;
  }
}
.more {
  height: 16.667vw;
  // background: pink;
  // text-align: center;
  display: flex;
  justify-content: center;
  p {
    border: 0.278vw solid #000;
    width: 27.778vw;
    height: 8.333vw;
    line-height: 8.333vw;
    border-radius: 4.167vw;
    font-size: 3.889vw;
    text-align: center;
  }
}
.mytype2 {
  .video {
    width: 100%;
    height: 50vw;
    position: relative;
    span {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 16.667vw;
      height: 16.667vw;
      background: rgba(0, 0, 0, 0.3);
      border-radius: 50%;
      text-align: center;
      line-height: 16.667vw;
      font-size: 13.889vw;
      color: #ffffff;
    }
  }
  .header {
    padding: 0 4.167vw;
    height: 13.889vw;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .info {
      display: flex;
      align-items: center;
      font-size: 3.889vw;
      color: #888;
      img {
        width: 8.333vw;
        height: 8.333vw;
        object-fit: cover;
        border-radius: 50%;
        margin-right: 1.111vw;
      }
    }
    .focus {
      background: #eee;
      padding: 1.389vw 2.778vw;
      border-radius: 4.167vw;
      font-size: 3.333vw;
    }
  }
  .content {
    padding: 1.389vw 4.167vw;
    p {
      margin-top: 2.778vw;
      font-size: 4.444vw;
      line-height: 6.944vw;
    }
  }
}
.myfocus {
  background: red !important;
  color: #fff;
}
/deep/ img {
  width: 100%;
  height: auto;
}
.dianzan {
  color: skyblue;
}
</style>